package com.decelo.jpa.entity.sys;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;
import java.util.Set;

@Entity
@Table(name = "user")
//@Cacheable
//@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "entityCache") //添加Cache*/
//@JsonIgnoreProperties({ "handler","hibernateLazyInitializer" })
public class User implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name="name")
    @Size(min = 4,max = 8,message = "name长度必须在4-8")
    @NotBlank(message ="name不能为空")
    private String name;

    @Column(name="possword")
    @NotNull
    private String possword;

    @Column(name="authenticator")
    private String authenticator;

    @Column(name="salt")
    private String salt;

    @Column(name="head_url")
    private String headUrl;

    @Column(name="status")
    private Integer status;

    @Column(name="email")
    @NotBlank(message ="email 不能为空")
    @Email(message ="email 不合法，例：xxxx@163.com")
    private String email;
    //可向配置 亦可双向配置
    @OneToOne(cascade =CascadeType.ALL)
    @JoinColumn(name="c_id")
    private Classes classes;

    @Column(name="jwt_token")
    //@PrimaryKeyJoinColumn
    private String jwttoken;

    @Column(name="create_time")
    @Temporal(TemporalType.TIMESTAMP)
    private Date creetaTime;

    @Column(name="update_time")
    @Temporal(TemporalType.TIMESTAMP)
    private Date updateTime;

    //映射单向一对多的关联关系
    //使用@OneToMany 来映射一对多的关联关系
    //使用@JoinColumn 来映射外键列的名称
    //可以使用@OneToMany的fetch 属性来修改默认的加载策略
    //可以通过@OneToMany的cascade 属性来修改默认的删除策略
    //cascade={CascadeType.REMOVE} 会把主表和从表的数据都删除
    //mappedBy表明放弃关联关系维护并且不能在使用
    //注意：若在一的一端@oneToMany 中使用mapperBy属性，则@oneToMany端就不能在使用@JoinColumn(name="u_id")属性
    /*@OneToMany(cascade=CascadeType.ALL,fetch =FetchType.LAZY,mappedBy="user")
    private Set<Role> roles;*/

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name="user_role",joinColumns =@JoinColumn(name="user_id"),
            inverseJoinColumns =@JoinColumn(name="role_id"))
    private Set<Role> roles;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Classes getClasses() {
        return classes;
    }

    public void setClasses(Classes classes) {
        this.classes = classes;
    }

    public Date getCreetaTime() {
        return creetaTime;
    }

    public void setCreetaTime(Date creetaTime) {
        this.creetaTime = creetaTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

    public String getPossword() {
        return possword;
    }

    public void setPossword(String possword) {
        this.possword = possword;
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    public void setHeadUrl(String headUrl) {
        this.headUrl = headUrl;
    }
    public String getHeadUrl() {
        return headUrl;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getJwttoken() {
        return jwttoken;
    }

    public void setJwttoken(String jwttoken) {
        this.jwttoken = jwttoken;
    }

    public String getAuthenticator() {
        return authenticator;
    }

    public void setAuthenticator(String authenticator) {
        this.authenticator = authenticator;
    }
}
